﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.ApllicationDto;

namespace Zocono.WCS.Domain.StationDomain
{
    public class StationInfoService : IStationInfoService
    {
        private readonly ILogUtil _LogUtil;
        private readonly ISnowflake _Snowflake;
        private readonly IStationInfoRepository _StationInfoRepository;
        public StationInfoService(ILogUtil logUtil
            , ISnowflake snowflake
            , IStationInfoRepository stationInfoRepository)
        {
            _LogUtil = logUtil;
            _Snowflake = snowflake;
            _StationInfoRepository = stationInfoRepository;
        }

        /// <summary>
        /// 创建工站信息
        /// </summary>
        /// <param name="stationInfo"></param>
        /// <returns></returns>
        public async Task<bool> CreateStationInfoAsync(StationInfo stationInfo)
        {
            stationInfo.ID = _Snowflake.GetId();
            bool isOk = await _StationInfoRepository.InsertAsync(stationInfo);

            return isOk;
        }

        /// <summary>
        /// 删除工站
        /// </summary>
        /// <param name="stationInfoList"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<bool> DeleteStationInfoAsync(List<StationInfo> stationInfoList)
        {
            try
            {
                _StationInfoRepository.AsTenant().BeginTran();

                foreach (var item in stationInfoList)
                {
                    await _StationInfoRepository.DeleteByIdAsync(item.ID);
                }

                _StationInfoRepository.AsTenant().CommitTran();

                return true;
            }
            catch (Exception ex)
            {
                _StationInfoRepository.AsTenant().RollbackTran();

                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 通过ID删除
        /// </summary>
        /// <param id="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteStationInfoByIdAsync(long id)
        {
            var res = await _StationInfoRepository.DeleteByIdAsync(id);
            return res;
        }
        /// <summary>
        /// 直接获取所有列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<StationInfo>> GetAllStationInfoAsync()
        {
            return await _StationInfoRepository.GetListAsync();
        }

        public async Task<List<StationInfo>> GetFilterStationInfoListAsync(string keywork)
        {
            var exp = Expressionable.Create<StationInfo>();
            exp.AndIF(!string.IsNullOrEmpty(keywork), it => it.StationCode.Contains(keywork));
            exp.AndIF(!string.IsNullOrEmpty(keywork), it => it.StationName.Contains(keywork));

            var list = await _StationInfoRepository.GetListAsync(exp.ToExpression());

            return list;
        }

        public async Task<PageDto<StationInfo>> GetPagedStationInfoAsync(QueryStationInfoDto queryStationInfo)
        {
            var exp = Expressionable.Create<StationInfo>();
            exp.AndIF(!string.IsNullOrEmpty(queryStationInfo.StationCode), it => it.StationCode.Contains(queryStationInfo.StationCode));
            exp.AndIF(!string.IsNullOrEmpty(queryStationInfo.StationName), it => it.StationName.Contains(queryStationInfo.StationName));

            PageDto<StationInfo> pagedData = new PageDto<StationInfo>();
            RefAsync<int> totalCount = 0;

            var result = await _StationInfoRepository.AsSugarClient()
                .Queryable<StationInfo>()
                .Where(exp.ToExpression())
                .ToPageListAsync(queryStationInfo.PageIndex, queryStationInfo.PageSize, totalCount);

            pagedData.Result = result;
            pagedData.Total = totalCount;

            return pagedData;
        }
        /// <summary>
        /// 根据ID获取工位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<StationInfo> GetStationInfoByIdAsync(long id)
        {
            return await _StationInfoRepository.GetByIdAsync(id);
        }

        public async Task<bool> ModifyStationInfoAsync(StationInfo stationInfo)
        {
            bool isOk = await _StationInfoRepository.UpdateAsync(stationInfo);

            return isOk;
        }
    }
}
